package com.hmall.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;


//@Component
public class DemoGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        //获取请求路径
        System.out.println("request.getURI().getPath() = " + request.getURI().getPath());
        System.out.println("request.getHeaders().getFirst(\"User-Agent\") = " + request.getHeaders().getFirst("User-Agent"));
        //响应401
        response.setRawStatusCode(401);
        return response.setComplete();
        //return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
